﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;

namespace Com
{
	/// <summary>
	/// Interaction logic for ucColdStorageLocation.xaml
	/// </summary>
	public partial class ucColdStorageLocation : UserControl
	{
        LibraryManagerInCom lm = new LibraryManagerInCom();
        Boolean cbxChange = true;
		public ucColdStorageLocation()
		{
			this.InitializeComponent();
		}

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {           
            enableButtons();
            loadData();
        }

        private void loadData()
        {
            cbxChange = true;
            cbxDescription.Items.Clear();
            LibraryManagerInCom.loadLocation();
            //cbxDescription.ItemsSource = LibraryManagerInCom.LocationList;
            foreach (var item in LibraryManagerInCom.LocationList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "location";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxDescription.SelectedItem != null && cbxChange)
            {
                txtAverage.Text = ((tb_location)cbxDescription.SelectedItem).average_temperature.ToString();
                txtMax.Text = ((tb_location)cbxDescription.SelectedItem).max_temperature.ToString();

                txtAverage_Copy.Text = ((tb_location)cbxDescription.SelectedItem).average_humidity.ToString();
                txtMax_Copy.Text = ((tb_location)cbxDescription.SelectedItem).max_humidity.ToString();
            }
            else
            {
                txtAverage.Text = "";
                txtMax.Text = "";

                txtAverage_Copy.Text = "";
                txtMax_Copy.Text = "";
            }
        }

        private void enableButtons()
        {
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnCopy.IsEnabled = true;
            btnDelete.IsEnabled = true;

     
        }
        private void disableButtons()
        {
            btnSave.IsEnabled = true;
            btnCancel.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnCopy.IsEnabled = false;
            btnDelete.IsEnabled = false;

            cbxChange = false;
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxDescription.Text = "";
            txtAverage.Text = "";
            txtAverage_Copy.Text = "";
            txtMax.Text = "";
            txtMax_Copy.Text = "";
            disableButtons();
            

        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_location lc = (tb_location)cbxDescription.SelectedItem;
            tb_location lc2 = LibraryManagerInCom.copyLocation(lc);
            cbxDescription.Text = lc2.location;
            txtMax.Text = lc2.max_temperature.ToString();
            txtMax_Copy.Text = lc2.max_humidity.ToString();
            txtAverage.Text = lc2.average_temperature.ToString();
            txtAverage_Copy.Text = lc2.average_humidity.ToString();
            disableButtons();

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                try
                {
                    tb_location lc = (tb_location)cbxDescription.SelectedItem;
                    LibraryManagerInCom.delLocation(lc);
                   
                    // Cập nhật lại dữ liệu cho control
                    loadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
   
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tb_location lc = new tb_location();
                lc.location = cbxDescription.Text;
                lc.max_temperature = Convert.ToDouble(txtMax.Text);
                lc.max_humidity = Convert.ToDouble(txtMax_Copy.Text);
                lc.average_humidity = Convert.ToDouble(txtAverage_Copy.Text);
                lc.average_temperature = Convert.ToDouble(txtAverage.Text);
                LibraryManagerInCom.addLocation(lc);
                // Refesh form
                enableButtons();
                loadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
         
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            enableButtons();
            loadData();
        }
	}
}